﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Gildor.SimpleHostMonitor.Desktop.Entities;

namespace Gildor.SimpleHostMonitor.Desktop.Utilities
{
    /// <summary>
    /// Loads host list from the "hosts.txt" file.
    /// </summary>
    class ConfigFileLoader
    {
        private static readonly string _profileFolderPath =
            Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData) +
            "\\Simple HostMonitor\\";

        private static readonly string _configFile = _profileFolderPath + "hosts.xml";

        /// <summary>
        /// Loads the host list.
        /// </summary>
        /// <returns>The loaded host list.</returns>
        [Obsolete]
        public static HostInfo[] Load ()
        {
            try
            {
                return File.ReadAllLines ("hosts.txt", Encoding.Default)
                    .Where (p => p.Trim ().Length != 0)
                    .Select (p => new HostInfo { Address = p })
                    .ToArray ();
            }
            catch (Exception)
            {
                return new HostInfo[0];
            }
        }

        /// <summary>
        /// Saves the specified hosts.
        /// </summary>
        /// <param name="hosts">The hosts.</param>
        [Obsolete]
        public static void Save (IEnumerable<HostInfo> hosts)
        {
            File.WriteAllLines ("hosts.txt", hosts.Select (p => p.Address).ToArray (), Encoding.Default);
        }

        private static readonly XmlSerializer _hostsConfigSerializer = new XmlSerializer (typeof (HostsConfiguration));

        /// <summary>
        /// Loads the configuration file.
        /// </summary>
        /// <returns>The hosts configuration.</returns>
        public static HostsConfiguration LoadNew ()
        {
            try
            {
                using (var stream = File.OpenRead (_configFile))
                {
                    var config = (HostsConfiguration)_hostsConfigSerializer.Deserialize (stream);
                    return config;
                }
            }
            catch (Exception)
            {
                return HostsConfiguration.Empty;
            }
        }

        ///// <summary>
        ///// Saves the configuration to a file.
        ///// </summary>
        ///// <param name="hosts">The hosts.</param>
        ///// <param name="defaultHost">The default host setting.</param>
        //public static void SaveNew (IEnumerable<HostInfo> hosts, HostInfo defaultHost)
        //{
        //    var config = new HostsConfiguration (hosts, defaultHost);

        //    SaveNew (config);
        //}

        /// <summary>
        /// Saves the configuration to a file.
        /// </summary>
        /// <param name="config">The <see cref="HostsConfiguration"/> 
        /// instance containing the configuration.</param>
        public static void SaveNew (HostsConfiguration config)
        {
            try
            {
                doSave (_hostsConfigSerializer, config);
            }
            catch (DirectoryNotFoundException)
            {
                //If the profile folder does not exist, create it first.
                Directory.CreateDirectory (_profileFolderPath);
                doSave (_hostsConfigSerializer, config);
            }
        }

        private static void doSave (XmlSerializer xSerializer, HostsConfiguration config)
        {
            using (var stream = File.Open (_configFile, FileMode.Create, FileAccess.Write))
            {
                xSerializer.Serialize (stream, config);
            }
        }
    }
}
